let _scope = {}
_scope.newWgt = function (curWidget) {
            _scope.curWidget = {};
            if (curWidget) {
                _scope.curWidget = curWidget;
            }
            _scope.curWidget.config = {};
            _scope.curWidget.config.option = {};
            _scope.curWidget.expressions = [];
            _scope.curWidget.filterGroups = [];
            _scope.curWidget.query = {};
            _scope.datasource = null;
            _scope.widgetName = null;
            _scope.widgetCategory = null;
            _scope.widgetId = null;
            _scope.optFlag = 'new';
            _scope.customDs = false;
            _scope.schema = null;
            _scope.liteMode = false;
            cleanPreview();
            addValidateWatch();
        };

        _scope.editWgt = function (widget) {
            $http.post("dashboard/checkWidget.do", {id: widget.id}).success(function (response) {
                if (response.status == '1') {
                    doEditWgt(widget);
                    if (_scope.customDs == true) _scope.doConfigParams();
                } else {
                    var d = widget.data.datasetId ? 'CONFIG.WIDGET.DATASET' : 'CONFIG.WIDGET.DATA_SOURCE';
                    ModalUtils.alert(translate("ADMIN.CONTACT_ADMIN") + "：" + translate(d) + '/' + response.msg, "modal-danger", "lg");
                }
            });
        };
        
        var doEditWgt = function (widget) {
            cleanPreview();
            $timeout(function () {
                switchNode(widget.id)
            }, 500);
            switchNode(widget.id);
            $('#preview_widget').html('');
            _scope.curWidget = angular.copy(widget.data);
            if (!_scope.curWidget.expressions) {
                _scope.curWidget.expressions = [];
            }
            if (!_scope.curWidget.filterGroups) {
                _scope.curWidget.filterGroups = [];
            }
            updateService.updateConfig(_scope.curWidget.config);
            _scope.datasource = _.find(_scope.datasourceList, function (ds) {
                return ds.id == widget.data.datasource;
            });
        
            _scope.widgetName = angular.copy(widget.categoryName + "/" + widget.name);
        
            _scope.widgetId = widget.id;
            _scope.optFlag = 'edit';
            _scope.customDs = _.isUndefined(_scope.curWidget.datasetId);
            loadDataset(function () {
                loadDsExpressions();
                loadDsFilterGroups();
                buildSchema();
                dataService.linkDataset(_scope.curWidget.datasetId, _scope.curWidget.config);
            });
            addWatch();
        };

export default {
  ..._scope
}
